<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addRoleVisible = true">新增角色</el-button>
            <el-table
              :data="roleData"
              border
              style="width: 100%;margin-top:15px;"
            >
              <el-table-column
                label="序号"
                type="index"
                width="150px"
              />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="{ row }">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="queryRole.page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="queryRole.pagesize"
              layout="total, sizes, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :offset="3" :span="8">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input v-model="companyInfo.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="companyInfo.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="companyInfo.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="companyInfo.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="companyInfo.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色弹层 -->
    <el-dialog
      :title="addRoleForm.id?'编辑角色':'新增角色'"
      :visible.sync="addRoleVisible"
      width="50%"
      @close="addDialogClose"
    >
      <el-form ref="form" :model="addRoleForm" :rules="roleForm" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addRoleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSava">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo } from '@/api/company.js'
import { getRole, addRole, updateRole } from '@/api/role.js'

export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      companyInfo: {},
      roleData: [],
      queryRole: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      addRoleVisible: false,
      addRoleForm: {
        name: '',
        description: ''
      },
      roleForm: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.loadCompanyInfo()
    this.loadRole()
  },
  methods: {
    async loadCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.companyInfo = res
    },
    // 获取所有角色列表
    async loadRole() {
      const res = await getRole(this.queryRole)
      this.roleData = res.rows
      this.total = res.total
    },
    handleSizeChange(val) {
      this.queryRole.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      this.queryRole.page = val
      this.loadRole()
    },
    // 点击了确定
    async onSava() {
      await this.$refs.form.validate()
      try {
        // 编辑
        if (this.addRoleForm.companyId) {
          await updateRole(this.addRoleForm)
          this.$message.success('编辑成功')
        } else {
          // 新增
          await addRole(this.addRoleForm)
          this.$message.success('添加成功')
        }
        this.addRoleVisible = false
        this.loadRole()
      } catch (err) {
        console.log(err)
      }
    },
    // 关闭弹层时清空数据和校验规则
    addDialogClose() {
      this.$refs.form.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 点击修改
    onEdit(row) {
      this.addRoleVisible = true
      this.addRoleForm = { ...row }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-tabs__header {
  .el-tabs__item {
    width: 140px;
    font-size: 18px;
    text-align: center;
  }
}
</style>
